﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System.Windows;
using ImageAppViewModels;
using BrainTechLLC.FolderExplorer;
using BrainTechLLC;
using BrainTechLLC.WPF;
using System.Windows.Input;
using System.Collections.Generic;
using DirectoryViewModels;
using System;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for DirectoriesViewWindow.xaml
    /// </summary>
    public partial class DirectoriesViewWindow : Window
    {
        public event EventHandler RequestClose;

        public static readonly DependencyProperty FileImageCollectionViewModelProperty =
        DependencyProperty.Register("FileImageCollectionViewModel", typeof(FileImageCollectionViewModel), typeof(DirectoriesViewWindow), new PropertyMetadata(null, OnFileImageCollectionViewModelPropertyChanged));

        public FileImageCollectionViewModel FileImageCollectionViewModel
        {
            get { return (FileImageCollectionViewModel)GetValue(FileImageCollectionViewModelProperty); }
            set { SetValue(FileImageCollectionViewModelProperty, value); }
        }

        private static void OnFileImageCollectionViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DirectoriesViewWindow source = d as DirectoriesViewWindow;
            FileImageCollectionViewModel vNew = e.NewValue as FileImageCollectionViewModel;
            FileImageCollectionViewModel vOld = e.OldValue as FileImageCollectionViewModel;
            source.UpdateFileImageCollectionViewModel(vNew, vOld);
        }

        private void UpdateFileImageCollectionViewModel(FileImageCollectionViewModel viewModel, FileImageCollectionViewModel viewModelOld)
        {
            FileImageCollectionViewModel = viewModel;
            directoriesView.DirectoriesToExamineViewModel = FileImageCollectionViewModel.DirectoriesToExamine;
            this.DataContext = FileImageCollectionViewModel;
        }

        public static readonly DependencyProperty TextSizeProperty =
            DependencyProperty.Register("TextSize", typeof(double), typeof(DirectoriesViewWindow), new PropertyMetadata(9.0, OnTextSizePropertyChanged));

        public double TextSize
        {
            get { return (double)GetValue(TextSizeProperty); }
            set { SetValue(TextSizeProperty, value); }
        }

        private static void OnTextSizePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            DirectoriesViewWindow source = d as DirectoriesViewWindow;
            double v = (double)e.NewValue;
            source.UpdateTextSize(v);
        }

        private void UpdateTextSize(double val)
        {
            UpdateUI();
        }

        public void UpdateUI()
        {
            directoriesView.FontSize = TextSize;
        }

        public DirectoriesViewWindow()
        {
            InitializeComponent();
        }

        public void WireUpSizeChanged()
        {
            this.SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
            this.LocationChanged += new EventHandler(Window_LocationChanged);
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            Window1.MainWindow.SaveDirectoriesViewSizeAndLocation();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Window1.MainWindow.SaveDirectoriesViewSizeAndLocation();
            RequestClose.RaiseEvent(this);
        }

        private void Window_LocationChanged(object sender, System.EventArgs e)
        {
            Window1.MainWindow.SaveDirectoriesViewSizeAndLocation();
        }

        private void Window_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                RequestClose.RaiseEvent(this);
            }
        }
        
    }
}
